
package jpoco.internal.xstream;

import jpoco.client.*;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.extended.ToStringConverter;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
// import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.io.xml.XppDriver;

public class XStreamFactory
{
	
	public static XStream createXStream(Format fmt, Class<? extends Response> responseClass) 
	{
		
		XStream xstream;
		
		if (fmt == Format.XML)
		{
			xstream = new XStream(new XppDriver());
		}
		else
		{
			xstream = new XStream(new JettisonMappedXmlDriver());
		}
		
 		xstream.registerConverter(new CalendarConverter(), XStream.PRIORITY_VERY_HIGH);
 		
 		try
 		{
 			xstream.registerConverter(new ToStringConverter(Relationship.class), XStream.PRIORITY_VERY_HIGH);
 		}
 		catch (NoSuchMethodException ex)
 		{
 			throw new RuntimeException(ex);
 		}
 		
 		if (responseClass.equals(Response.MultipleContactResponse.class))
 		{
			xstream.addImplicitCollection(responseClass, "contacts", "entry", Contact.class);
			xstream.aliasField("entry", responseClass, "contacts");
			xstream.addImplicitCollection(responseClass, "contacts", "person", Contact.class);
			xstream.aliasField("person", responseClass, "contacts");
 		}
 		else if (responseClass.equals(Response.SingleContactResponse.class))
 		{
 			xstream.aliasField("entry", responseClass, "contact");
 		}
		
		
 		xstream.addImplicitCollection(Contact.class, "emails", Email.class);
 		xstream.addImplicitCollection(Contact.class, "urls", Url.class);
 		xstream.addImplicitCollection(Contact.class, "instantMessagingAddresses", InstantMessagingAddress.class);
 		xstream.addImplicitCollection(Contact.class, "addresses", Address.class);
 		xstream.addImplicitCollection(Contact.class, "photos", Photo.class);
 		xstream.addImplicitCollection(Contact.class, "accounts", Account.class);
 		xstream.addImplicitCollection(Contact.class, "phoneNumbers", PhoneNumber.class);
 		xstream.addImplicitCollection(Contact.class, "organizations", Organization.class);
 		xstream.addImplicitCollection(Contact.class, "relationships", Relationship.class);
 		xstream.addImplicitCollection(Contact.class, "activities", Activity.class);
 		xstream.addImplicitCollection(Contact.class, "books", String.class);
 		xstream.addImplicitCollection(Contact.class, "music", String.class);
 		xstream.addImplicitCollection(Contact.class, "movies", String.class);
 		xstream.addImplicitCollection(Contact.class, "tvShows", String.class);
 		xstream.addImplicitCollection(Contact.class, "interests", String.class);
 		xstream.addImplicitCollection(Contact.class, "heroes", String.class);
 		
		xstream.aliasField("accounts", Contact.class, "accounts");
		xstream.aliasField("addresses", Contact.class, "addresses");
		xstream.aliasField("ims", Contact.class, "instantMessagingAddresses");
		xstream.aliasField("phoneNumbers", Contact.class, "phoneNumbers");
		xstream.aliasField("urls", Contact.class, "urls");
		xstream.aliasField("emails", Contact.class, "emails");
		xstream.aliasField("organizations", Contact.class, "organizations");
		xstream.aliasField("photos", Contact.class, "photos");
		xstream.aliasField("relationships", Contact.class, "relationships");
		xstream.aliasField("activities", Contact.class, "activities");
		xstream.aliasField("books", Contact.class, "books");
		xstream.aliasField("music", Contact.class, "music");
		xstream.aliasField("movies", Contact.class, "movies");
		xstream.aliasField("tvShows", Contact.class, "tvShows");
		xstream.aliasField("interests", Contact.class, "interests");
		xstream.aliasField("heroes", Contact.class, "heroes");
		xstream.aliasField("dateOfBirth", Contact.class, "birthday"); // note:  MySpace returns 'dateOfBirth' when they should return 'birthday'
		xstream.aliasField("street", Address.class, "streetAddress"); // note:  MySpace returns 'street' when they should return 'streetAddress'

		xstream.alias("response", responseClass);
		xstream.alias("activities", Activity.class);
		xstream.alias("entry", Contact.class);
		xstream.alias("id", String.class);
		xstream.alias("locality", String.class);
		xstream.alias("region", String.class);
		xstream.alias("country", String.class);
		xstream.alias("postalCode", String.class);
		xstream.alias("formatted", String.class);
		xstream.alias("name", String.class);
		xstream.alias("title", String.class);
		xstream.alias("domain", String.class);
		xstream.alias("userid", String.class);
		xstream.alias("username", String.class);
		xstream.alias("birthday", java.util.Calendar.class);
		xstream.alias("books", String.class);
		xstream.alias("music", String.class);
		xstream.alias("movies", String.class);
		xstream.alias("tvShows", String.class);
		xstream.alias("interests", String.class);
		xstream.alias("heroes", String.class);
		xstream.alias("value", String.class);
		xstream.alias("streetAddress", String.class);
		xstream.alias("street", String.class);
		xstream.alias("type", String.class);
		xstream.alias("primary", Boolean.class);
		xstream.alias("urls", Url.class);
		xstream.alias("emails", Email.class);
		xstream.alias("photos", Photo.class);
		xstream.alias("relationships", Relationship.class);
		xstream.alias("ims", InstantMessagingAddress.class);
		xstream.alias("addresses", Address.class);
		xstream.alias("organizations", Organization.class);
		xstream.alias("accounts", Account.class);
		xstream.alias("phoneNumbers", PhoneNumber.class);
		xstream.alias("ims", InstantMessagingAddress.class);
		xstream.alias("networkPresence", NetworkPresence.class); // OpenSocial only

		xstream.alias("totalResults", Integer.class);
		xstream.alias("startIndex", Integer.class);
		xstream.alias("itemsPerPage", Integer.class);
		xstream.alias("age", Integer.class);

		xstream.alias("isOwner", Boolean.class);
		xstream.alias("isViewer", Boolean.class);
		
		return xstream;
		
	}
}
